package chapter14.chapter14_4.test15;

/**
 * Created by Caesar on 2016/2/29.
 */

import chapter14.chapter14_3.*;
import chapter14.chapter14_4.factory.Factory;

import java.util.*;

public class PetCreator {
    private Random rand = new Random(47);

    public String toString() {
        return getClass().getSimpleName();
    }

    static List<Class<? extends Pet>> partFactories =
            new ArrayList<Class<? extends Pet>>();

    static {
        partFactories.add(Pet.class);
        partFactories.add(Dog.class);
        partFactories.add(Mutt.class);
        partFactories.add(Pug.class);
        partFactories.add(Cat.class);
        partFactories.add(EgyptianMau.class);
        partFactories.add(Manx.class);
        partFactories.add(Cymric.class);
        partFactories.add(Rodent.class);
        partFactories.add(Rat.class);
        partFactories.add(Mouse.class);
        partFactories.add(Hamster.class);
    }

    // The List of the different types of Pet to create:

    public Pet randomPet() { // Create one random Pet
        int n = rand.nextInt(partFactories.size());
        try {
            return partFactories.get(n).newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Pet[] createArray(int size) {
        Pet[] result = new Pet[size];
        for (int i = 0; i < size; i++)
            result[i] = randomPet();
        return result;
    }

    public ArrayList<Pet> arrayList(int size) {
        ArrayList<Pet> result = new ArrayList<Pet>();
        Collections.addAll(result, createArray(size));
        return result;
    }
}